<template>
    <div>
        <h1>添加门店</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td><span style="color:red;">*</span>店名</td>
                    <td>
                        <input type="text" v-model="quinfor.name">
                    </td>
                </tr>
                <tr>
                    <td><span style="color:red;">*</span>营业时间</td>
                    <td>
                        <input type="date" v-model="quinfor.create">
                    </td>
                </tr>
                <tr>
                    <td><span style="color:red;">*</span>服务范围</td>
                    <td>
                        <input type="text" v-model="quinfor.fuwu">
                    </td>
                </tr>
                <tr>
                    <td>店主</td>
                    <td>
                        <input type="text" v-model="quinfor.dianName">
                    </td>
                </tr>
                 <tr>
                    <td>联系电话</td>
                    <td>
                        <input type="text" v-model="quinfor.phone">
                    </td>
                </tr>
                <tr>
                    <td>门店介绍</td>
                    <td>
                       <textarea v-model="quinfor.jieShao">

                       </textarea>
                    </td>
                </tr>
                <tr>
                    <td><span style="color:red;">*</span>门店地址</td>
                    <td>
                        <input type="text" v-model="quinfor.addres">
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input
              type="radio"
              name="state"
              v-model="quinfor.state"
              value="1"
            />启用<input
              type="radio"
              name="state"
              v-model="quinfor.state"
              value="0"
            />禁用
                    </td>
                </tr>
                <tr>
                    <td>门店log</td>
                    <td>
                        <input type="file" @change="img" />
                        <img
                        :src="quinfor.log"
                        width="100"
                        height="100"
                        alt=""
                        v-if="quinfor.log != ''"
                        />
                    </td>
                </tr>
                <tr>
                    <td>门店图片：</td>
                    <td>
                        <input type="file" @change="img1" />
                        <img
                        :src="quinfor.photo"
                        width="100"
                        height="100"
                        alt=""
                        v-if="quinfor.photo != ''"
                        />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" style="border-color: aquamarine;" value="确定" @click="add">
                    </td>
                    <td>
                        <input type="button"  value="取消" @click="qu">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import {reactive,ref,onMounted} from 'vue'
import {useRoute,useRouter} from 'vue-router'
const router=useRouter();
let route=useRoute();
let quinfor=reactive({
  "name": "",
  "create": "",
  "fuwu": "",
  "dianName": "",
  "phone": "",
  "jieShao": "",
  "addres": "",
  "state": 0,
  "log": "",
  "photo": "",
  "isDelete": false
});

const add=()=>{
    if(quinfor.name==""){
        alert("店名不能为空!");
        return;
    }
    if(quinfor.create==null){
        alert("营业时间不能为空!");
        return;
    }
    if(quinfor.fuwu==""){
        alert("服务范围不能为空!");
        return;
    }
    if(quinfor.addres==""){
        alert("门店地址不能为空!");
        return;
    }

    axios({
        url:'https://localhost:7188/api/MenDian/Add',
        method:'post',
        data:quinfor
    })
    .then((res)=>{
        console.log(res);
        alert("添加成功!");
        router.push("/mendianshow")
    })
    .catch((err)=>{
        console.log(err);
        
    })
}

const img = (e: any) => {
  console.log(e);
  let obj = e.target.files[0];
  let f = new FormData();
  f.append('file', obj);
  axios({
    url: 'https://localhost:7188/api/MenDian/UpdateImg',
    method: 'post',
    data: f,
  })
    .then((res) => {
      console.log(res);
      if (res.data == '不是图片') {
        alert('上传的不是图片');
        return;
      }

      if (res.data == '大于2M') {
        alert('上传的图片大于2M');
        return;
      }
      quinfor.log = res.data;
    })
    .catch((err) => {
      console.log(err);
    });
};
const img1 = (e: any) => {
  console.log(e);
  let obj = e.target.files[0];
  let f = new FormData();
  f.append('file', obj);
  axios({
    url: 'https://localhost:7188/api/MenDian/UpdateImg1',
    method: 'post',
    data: f,
  })
    .then((res) => {
      console.log(res);
      if (res.data == '不是图片') {
        alert('上传的不是图片');
        return;
      }

      if (res.data == '大于2M') {
        alert('上传的图片大于2M');
        return;
      }
      quinfor.photo = res.data;
    })
    .catch((err) => {
      console.log(err);
    });
};
const qu=()=>{
    quinfor.name="";
    quinfor.create="";
    quinfor.fuwu="";
    quinfor.dianName="";
    quinfor.phone="";
    quinfor.jieShao="";
    quinfor.addres="";
    quinfor.state=0;
    quinfor.log="";
    quinfor.photo="";
}
</script>

<style scoped>

</style>